let app = getApp()
var {
  Api
} = require('../../api/api')
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    detail: { //题目详情
      type: Object,
      value: {},
      observer: 'watch1'
    },
    submit: { //是否提交
      type: Boolean,
      value: false,
      observer: 'watch'
    },
    showDa: { //是否提交
      type: Boolean,
      value: false
    },
    vague: { //选项是否模糊
      type: Boolean,
      value: false
    },
    ye: { //选项是否模糊
      type: Boolean,
      value: false
    },
    my_answer: { //我选择的答案
      type: Object,
      value: [],
    },
    items: {
      type: Object,
      value: {}
    },
    num: { //题目
      type: Number,
      value: 0
    },
    type: { //1为章节列表进的2就是考试//3為错题本进来的//4是直接重笔记本里和考同学进来的
      type: Number,
      value: 0
    },
    isOnExam: { //题目
      type: Boolean,
      value: false
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    option_json: [],
    domain: Api.domain,
    subjectInfo:{}
  },

  moved() {},
  /**
   * 组件的方法列表
   */
  methods: {
    //获取题目详情
    get_mk_subject_info(subject_id,report_id){
      console.log("787883487yuuyuyere");
      console.log("787883487yuuyuyere",report_id);
      console.log("787883487yuuyuyere");
      wx.$request(wx.$api.mk_subject_info,{
        subject_id,
        report_id
      }).then(res=>{
        console.log("1111212121212");
        console.log(res);
        console.log("1111212121212");
          this.setData({
            subjectInfo:res
          })
      })
    },
    check(e) { //选择答案
      if (this.data.submit) {
        return
      }
      let {
        index
      } = e.currentTarget.dataset
      if (this.data.type == 2) { //考试，需要单独处理
        this.check2(index)
        return
      }
      let detail = this.data.detail
      if (detail.type == 1) { //单选时候
        for (let i = 0; i < detail.option_json.length; i++) {
          detail.option_json[i].is_choice = 2
        }
        detail.option_json[index].is_choice = 1
      } else { //多选时候
        detail.option_json[index].is_choice = detail.option_json[index].is_choice==1?2:1
        console.log(detail.option_json[index].is_choice, 'adsfasdfafasdfafasfadsf');
      }
      console.log(detail.option_json)
      this.triggerEvent('optionJson',detail.option_json)
      this.setData({
        detail
      })
    },
    check2(e) { //考试要记录考试选择的哪些提

      var pageArr = getCurrentPages();
      var thisPage = pageArr[pageArr.length - 1];
      let index = e
      let detail = this.data.detail
      if (detail.type == 1) { //单选时候
        for (let i = 0; i < detail.option_json.length; i++) {
          detail.option_json[i].is_choice = 2
        }
        detail.option_json[index].is_choice = 1
      } else { //多选时候
        detail.option_json[index].is_choice = detail.option_json[index].is_choice==1?2:1
      }
      
      this.setData({
        detail
      })
      
      /********************错题集**************/
      //let sids=
     
      //thisPage.data.items3

      console.log("222222222222222");
      console.log(this.data.detail);
      console.log("33333333333333");


      this.triggerEvent('optionJson',detail.option_json)
      let my_answer = []
      for (let i = 0; i < detail.option_json.length; i++) {
        if (detail.option_json[i].is_choice==1) {
          my_answer = my_answer.concat(detail.option_json[i].content)
        }
      }
      if (my_answer.length != 0) {
        thisPage.setData({
          [`items2[${this.data.num}].subject_log.my_answer`]: my_answer,
          [`items2[${this.data.num}].is_correct`]: 1 //记录这道题做了
        })
      }
      //console.log('9999999999999999',my_answer,'9999999999999999');
      console.log('12111111111',thisPage.data.items2, '7777777777777777777');
      console.log('888888888888888',thisPage.data.questionList, '执行了该方法');
    },
    watch1(newDate, oldDate) {
      console.log(newDate,'date555555555555555555')
      this.get_mk_subject_info(newDate.subject_id,newDate.report_id)
      this.setData({
        option_json: []
      })
      if (this.data.isOnExam && this.data.isSubmit) {
        this.closeVague()
        let my_answer = this.data.my_answer //我的答案
        let option_json = this.data.detail.option_json //选择题列表
        console.log(option_json, 'xxx')
        for (let i = 0; i < option_json.length; i++) {
          if (option_json[i].is_correct == 1) { //先给所有正确答案附上正确
            option_json[i].dui = true
          }
          for (let j = 0; j < my_answer.length; j++) {
            if (option_json[i].option == my_answer[j]) {
              if (option_json[i].is_correct == 1) {
                option_json[i].dui = true
              } else {
                option_json[i].cuo = true
              }
            }
          }
        }
        this.setData({
          option_json: option_json
        })
      }
    },
    watch(newDate, oldDate) { //监听是否已经提交答案了,然后赋值对错
      this.setData({
        option_json: []
      })
      console.log(newDate, 'newDate', oldDate)
      if (newDate) { //提交答案了
        this.closeVague()
        let my_answer = this.data.my_answer //我的答案
        let option_json = this.data.detail.option_json //选择题列表
        console.log(option_json, 'xxx')
        for (let i = 0; i < option_json.length; i++) {
          if (option_json[i].is_correct == 1) { //先给所有正确答案附上正确
            option_json[i].dui = true
          }
          for (let j = 0; j < my_answer.length; j++) {
            if (option_json[i].option == my_answer[j]) {
              if (option_json[i].is_correct == 1) {
                option_json[i].dui = true
              } else {
                option_json[i].cuo = true
              }
            }
          }
        }
        this.setData({
          option_json: option_json,
          isSubmit: true

        })
      }
    },
    closeVague() { //清除模糊用的
      var pageArr = getCurrentPages();
      var thisPage = pageArr[pageArr.length - 1];
      thisPage.setData({
        vague: false
      })
    },
  }
})
